using System;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;

namespace IntegratedMeasuring.Pages;

public partial class MainPage : UserControl
{
    public event EventHandler? LogoutRequested;
    private WorkbenchPage? _currentWorkbenchPage;

    public MainPage()
    {
        InitializeComponent();
        // 默认显示工作台页面
        ShowWorkbenchPage();
    }
    
    private void WorkbenchBtn_Click(object? sender, RoutedEventArgs e)
    {
        ShowWorkbenchPage();
        UpdateBreadcrumb("工作台");
        UpdateButtonStyles(WorkbenchBtn);
    }

    private void TestHistoryBtn_Click(object? sender, RoutedEventArgs e)
    {
        if (this.ContentArea != null)
            this.ContentArea.Content = new TestHistoryPage();
        UpdateBreadcrumb("测试历史");
        UpdateButtonStyles(TestHistoryBtn);
    }

    private void ModelManagementBtn_Click(object? sender, RoutedEventArgs e)
    {
        if (ContentArea != null)
            ContentArea.Content = new ModelManagementPage();
        UpdateBreadcrumb("机种管理");
        UpdateButtonStyles(ModelManagementBtn);
    }

    private void OrderManagementBtn_Click(object? sender, RoutedEventArgs e)
    {
        if (ContentArea != null)
            ContentArea.Content = new OrderManagementPage();
        UpdateBreadcrumb("工单管理");
        UpdateButtonStyles(OrderManagementBtn);
    }

    private void StatisticsBtn_Click(object? sender, RoutedEventArgs e)
    {
        if (ContentArea != null)
            ContentArea.Content = new StatisticsPage();
        UpdateBreadcrumb("统计分析");
        UpdateButtonStyles(StatisticsBtn);
    }

    private void OperationLogBtn_Click(object? sender, RoutedEventArgs e)
    {
        if (ContentArea != null)
            ContentArea.Content = new OperationLogPage();
        UpdateBreadcrumb("操作日志");
        UpdateButtonStyles(OperationLogBtn);
    }

    private void SystemSettingsBtn_Click(object? sender, RoutedEventArgs e)
    {
        if (ContentArea != null)
            ContentArea.Content = new SystemSettingsPage();
        UpdateBreadcrumb("系统设置");
        UpdateButtonStyles(SystemSettingsBtn);
    }

    private void ShowWorkbenchPage()
    {
        if (ContentArea != null)
        {
            _currentWorkbenchPage = new WorkbenchPage();
            ContentArea.Content = _currentWorkbenchPage;
        }
        UpdateButtonStyles(WorkbenchBtn);
    }

    private void UpdateButtonStyles(Button? activeButton)
    {
        if (activeButton == null) return;
        
        // 重置所有按钮样式
        var buttons = new[] { WorkbenchBtn, TestHistoryBtn, ModelManagementBtn, OrderManagementBtn, StatisticsBtn, OperationLogBtn, SystemSettingsBtn };
        foreach (var button in buttons)
        {
            if (button != null)
                button.Background = Avalonia.Media.Brushes.Transparent;
        }
        
        // 设置活动按钮样式
        activeButton.Background = new Avalonia.Media.SolidColorBrush(Avalonia.Media.Color.Parse("#34495E"));
    }

    // private void LogoutBtn_Click(object? sender, RoutedEventArgs e)
    // {
    //     LogoutRequested?.Invoke(this, EventArgs.Empty);
    // }

    private void BreadcrumbHome_Tapped(object? sender, Avalonia.Input.TappedEventArgs e)
    {
        ShowWorkbenchPage();
        UpdateBreadcrumb("工作台");
    }

    private void UpdateBreadcrumb(string currentPage)
    {
        if (BreadcrumbCurrent != null)
            BreadcrumbCurrent.Text = currentPage;
    }

    /// <summary>
    /// 热键触发处理方法（由主窗口调用）
    /// </summary>
    public void OnHotKeyPressed(object? sender, EventArgs e)
    {
        // 转发给当前的工作台页面
        if (_currentWorkbenchPage != null && "工作台".Equals(BreadcrumbCurrent.Text))
        {
            _currentWorkbenchPage.OnHotKeyPressed(sender, e);
        }
    }
} 